$(document).ready(function() {
	//预览二维码功能
	$('.operation_btn').click(function() {
		var box_id = this.id;
        var template = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri="+encodeURIComponent("http://117.78.6.144/wcpay?boxid="+box_id)+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";

        var html = "<div id='qrcode'></div>";
		$('#code').html(html);
		$('#code').show();
		$('#qrcode').qrcode({
			render: "table", //table方式 
			width: 200, //宽度
			height: 200, //高度
			text: template
		});
	});
	//点击按钮，调用浏览器打印
	$('.print').click(function() {
		var phoneNumber = this.name;
        var template = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri="+encodeURIComponent("http://117.78.6.144/wcpay?boxid="+phoneNumber)+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
		var html = "<div id='qrcode'></div>";
		$('#code').html(html+"<br><br>"+phoneNumber);
		$('#code').show();
		$('#qrcode').qrcode({
			render: "table", //table方式 
			width: 263, //宽度 
			height: 262.5, //高度 
			text: template
		});
		Print();
	});

	function Print() {
		bdhtml = window.document.body.innerHTML;
		sprnstr = "<!--startprint-->";
		eprnstr = "<!--endprint-->";
		prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
		prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
		window.document.body.innerHTML = prnhtml;
		window.print();
		window.location.reload();
	}
})

var sons = document.getElementsByName("son");
//反选框
function fanxuan() {
	for (var i = 0; i < sons.length; i++) {
		if (sons[i].checked) {
			sons[i].checked = "";
		} else {
			sons[i].checked = "checked";
		}
	}
}
//全选框
function changeAll() {
	var s = document.getElementsByName("son");
	for (var i = 0; i < s.length; i++) {
		s[i].checked = "checked";
	}
}

// function change() {
// 	var isTrue = true;
// 	for (var i = 0; i < sons.length; i++) {
// 		isTrue = (isTrue && sons[i].checked);
// 	}	
// }
//多选全选打印
function Allprint(){
    var checks=[];
    for (var i = 0; i < sons.length; i++) {
        if (sons[i].checked) {
            var box_id = sons[i].value.split(',')[0];
            var sim = sons[i].value.split(',')[1];
            var template = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri="+encodeURIComponent("http://117.78.6.144/wcpay?boxid="+box_id)+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
            console.log(template)
            var html = "<p id='qrcode'></p>";
            $('#Allcode').append("<p style='margin-bottom:9%'>"+html+"</p>"+sim+"<br/><br/>");
            $('#Allcode').show();
            $('#Allcode').qrcode({
                render: "table", //table方式
                width: 400, //宽度
                height: 400, //高度
                text: template
            });
            $.get("/box_management/code",{sim:sim});
        }else{
            checks.push(i);
            if(checks.length==sons.length){
                return false;
            }
        }
    }
    APrint();
}

function APrint() {
		bdhtml = window.document.body.innerHTML;
		sprnstr = "<!--++++++++++-->";
		eprnstr = "<!--========-->";
		prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
		prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
		window.document.body.innerHTML = prnhtml;
		window.print();
		window.location.reload();
	}
//	function state(obj){
//		var sim=obj.name;
//		$.get("/box_management/code",{sim:sim});
//	}